package com.example.webcomen.Service.impl;


import cn.hutool.log.Log;
import com.example.webcomen.Mapper.UserMapper;
import com.example.webcomen.Model.User;
import com.example.webcomen.Service.BaseInfoService;
import com.example.webcomen.Service.LoginService;
import com.example.webcomen.Utill.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class LoginServiceImpl implements LoginService {
    private static Log a=Log.get("info");
    @Autowired
    JavaMailSenderImpl mailSender;

    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    RedisCache redisCache;

    @Autowired
    BaseInfoService baseInfoService;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Override
    public void sendEmailCode(String email) {
        String code = String.valueOf(new Random().nextInt(899999) + 100000);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(email);
        message.setSubject("WebComEn系统邮件");
        message.setText("[WebComEn]验证码为：" + code + "\n 如非本人操作，请忽略！谢谢");
        a.info("验证码是"+code);
        mailSender.send(message);
        String verifyKey = "VerCode:" + email;
        redisCache.setCacheObject(verifyKey, code, 5, TimeUnit.MINUTES);
    }

    @Override
    public boolean validateEmailCode(String code, String email) {
        String verifyKey = "VerCode:" + email;
        String checkCode = redisCache.getCacheObject(verifyKey);
        if (checkCode == null || !checkCode.equalsIgnoreCase(code)) {
            return false;
        }
        redisCache.deleteObject(verifyKey);
        return true;
    }

    @Override
    public void validatePassword(String password, User user) {

    }

    @Override
    public List<GrantedAuthority> authorityList() {
        List<String> list = new ArrayList<>();
        list.add("ROLE_USER");
        return list.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }


    @Override
    public boolean registerNewUser(String email,String password,String nickname) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(passwordEncoder.encode(password));
        user.setUsername(nickname);
        user.setStatus("hide");
        user.setActive("activecode:daefda");
        user.setCreateDate(Date.valueOf(LocalDate.now()));
        return baseInfoService.addUser(user);
    }
}
